<?php
class Customer_management extends CI_Model{
	var $firstname = "";
	var $lastname = "";
	var $email = "";

	function __construct(){
	 // Call the Model constructor
        parent::__construct();
		$this->load->database();
	}

	function getCustomers(){
		$result = $this->db->query('select * from customer');
		return $result->result();
	}
	function getCustomer($cx_id){
		$query = "select * from customer where customerCode='".$cx_id."'";
		$result = $this->db->query($query);
		return $result->result();
	}

	function generateCustomerCode(){
		$this->db->select('customerCode');
		$this->db->order_by('customerCode desc');
		$result=$this->db->get('customer');
		if($result->num_rows() > 0){
			$lastCustomerCode = $result->row()->customerCode;
			$newCustomerCode = explode('IN',$lastCustomerCode);
			return 'IN'.($newCustomerCode[1]+1);
		}
		else{
			return 'IN1001';
		}
	}

	function generateSiteCode(){
		$this->db->select('siteCode');
		$this->db->order_by('sort desc');
		$result = $this->db->get('site');
		if($result->num_rows() > 0){
			$lastSiteCode = $result->row()->siteCode;
			$newSiteCode = explode('CS',$lastSiteCode);
			return 'CS'.($newSiteCode[1]+1);
		}
		else{
			return 'CS1';
		}
	}
	function createCustomer($data){
		$this->db->insert('customer',$data);
		return $this->db->affected_rows();
	}
	function createSite($data){
		$this->db->insert('site',$data);
		return $this->db->affected_rows();
	}
	function updateCustomer($data){
		$this->db->where('customerCode',$data['customerCode']);
		$this->db->update('customer',$data);
		return $this->db->affected_rows();
	}
	function getBranches($id){
		$query = "Select * from site where customerCode = '".$id."' and type = 'branch'";
		$result = $this->db->query($query);
		return $result->result();
	}
	function getHQ($id){
		$query = "Select * from site where customerCode = '".$id."' and type = 'head'";
		$result = $this->db->query($query);
		return $result->result();
	}

	/*
	function getCode()
	{
		$this->db->select('customerCode');
		$this->db->order_by('customerCode desc');
		$query=$this->db->get('customer');
		
		foreach($query->result() as $row ){
			return $row->customerCode;
			
		}
	}*/
	/*
	function create($data){
		$query = $this->db->insert('customer',$data);
		return $this->db->affected_rows();
	}
	
	function createSite(){
		$query = $this->db->insert('site');	
	}
	function getcode()
	{
		$this->db->order_by("customerCode", "desc"); 
		$this->db->select('customerCode');
		$query=$this->db->get('customer');
		
		foreach ($query->result() as $row)
		{
    		echo $row->customerCode.'<br />';
		
		}
	}*/
}
?>
